<?php

declare (strict_types=1);

namespace app\common\command;

use app\common\command\task\AiImportQuestion;
use app\common\command\task\BuildAiExplanation;
use app\common\command\task\BuildOne23PanSession;
use app\common\command\task\ExecuteSql;
use app\common\command\task\IndexEsCourseQuestionData;
use app\common\command\task\OrderBonusProcess;
use app\common\command\task\OrderBonusSettlementProcess;
use app\common\command\task\ProcessUserAnswerQueue;
use app\common\command\task\SendExceptionMessage;
use app\common\command\task\Test;
use app\common\command\task\Test1;
use app\common\command\task\IndexEsCourseSearchData;
use app\common\command\task\UserRanking;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class Cron extends Command
{
    protected function configure()
    {
        $this->setName('cron')
            ->addArgument('name', Argument::OPTIONAL, "任务名称")
            ->setDescription('cron');
    }

    /**
     * getTaskContainer
     * @return string[]
     */
    public function getTaskContainer(): array
    {
        return $this->taskContainer;
    }

    /**
     * 子指令集合
     * @var string[]
     */
    protected array $taskContainer = [
        // 测试脚本:php think cron test
        'test' => Test::class,
        // 测试脚本:php think cron test1
        'test1' => Test1::class,
        // 发送异常信息:php think cron sendExceptionMessage
        'sendExceptionMessage' => SendExceptionMessage::class,
        // 生成订单分红:php think cron orderBonusProcess
        'orderBonusProcess' => OrderBonusProcess::class,
        // 生成订单分红结算单:php think cron orderBonusSettlementProcess
        'orderBonusSettlementProcess' => OrderBonusSettlementProcess::class,
        // 学霸排行数据生成:php think cron userRanking
        'userRanking' => UserRanking::class,
        // 定时执行SQL语句:php think cron executeSql
        'executeSql' => ExecuteSql::class,
        // 生成ai解析:php think cron buildAiExplanation
        'buildAiExplanation' => BuildAiExplanation::class,
        // 生成123云盘session:php think cron buildOne23PanSession
        'buildOne23PanSession' => BuildOne23PanSession::class,
        // Ai导入题目:php think cron aiImportQuestion
        'aiImportQuestion' => AiImportQuestion::class,
        // 索引es课程数据:php think cron indexCourseSearchData
        'indexCourseSearchData' => IndexEsCourseSearchData::class,
        // 索引es题目数据:php think cron indexEsCourseQuestionData
        'indexEsCourseQuestionData' => IndexEsCourseQuestionData::class,
        // 处理用户答题消息队列:php think cron processUserAnswerQueue
        'processUserAnswerQueue' => ProcessUserAnswerQueue::class,
    ];

    /**
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        // 请求参数
        $name = trim($input->getArgument('name'));

        // 检查是否存在任务
        if (!isset($this->taskContainer[$name])) {
            $output->writeln("任务{$name}不存在");
            exit();
        }

        // 提取Class
        $class = $this->taskContainer[$name];

        // 执行任务
        try {
            $reflect = new \ReflectionClass($class);
            $reflectFuncName = 'invoke';
            if (!$reflect->hasMethod($reflectFuncName)) {
                $output->writeln("任务{$name}必须包含{$reflectFuncName}");
            }
            $object = new $class();
            call_user_func([$object, $reflectFuncName]);
            echo '执行' . $class . '完成' . PHP_EOL;
        } catch (\Exception $e) {
            file_put_contents('err.txt',json_encode($e->getTrace()));
            $output->writeln("执行任务{$name}时异常:" . $e->getMessage() . "，文件位置" . $e->getFile() . ',行号' . $e->getLine());
        }
    }
}
